#include "wap_processbar.h"
#include "wa_wm.h"
#include "wa_win.h"
#include "wa_screen.h"
#include "wa_processbar.h"
#include "wa_label.h"
#include "wap.h"
#include "stdio.h"

static void wap_processbar_create(void);
static void wap_processbar_del(void);
static void wap_processbar_callback(wa_sign* sign);

wa_screen_obj wap_processbar_screen = 
{
	.id = WAP_WIN_PROCESSBAR_ID,
	.create = wap_processbar_create,
	.del = wap_processbar_del,
};

wa_obj* wap_processbar_win;
wa_obj* wap_processbar_processbar;
wa_obj* wap_processbar_label_title;
wa_obj* wap_processbar_label_value;

extern wa_font font_song_16_ascii;

uint8_t wap_processbar_value;

void wap_processbar_init(void)
{
	wap_processbar_value = 0;
}

static void wap_processbar_create(void)
{
	char str[4];
	wap_processbar_win = wa_win_create(NULL);
	wa_win_set_pos(wap_processbar_win, 0, 0);
	wa_win_set_size(wap_processbar_win, WA_LCD_W, WA_LCD_H);
	wa_win_set_edge_show(wap_processbar_win, wa_nok);
	wa_win_set_callback(wap_processbar_win, wap_processbar_callback);
	wap_processbar_screen.win = wap_processbar_win;
	
	wap_processbar_label_title = wa_label_create(NULL);
	wa_win_add_obj(wap_processbar_win, wap_processbar_label_title);
	wa_obj_set_pos(wap_processbar_label_title, 0, 8);
	wa_obj_set_size(wap_processbar_label_title, WA_LCD_W, 16);
	wa_label_set_font(wap_processbar_label_title, &font_song_16_ascii);
	wa_label_set_text(wap_processbar_label_title, "processbar");
	wa_label_set_align_mode(wap_processbar_label_title, WA_LABEL_AL_HCENTER | WA_LABEL_AL_VCENTER);
	
	wap_processbar_label_value = wa_label_create(NULL);
	wa_win_add_obj(wap_processbar_win, wap_processbar_label_value);
	wa_obj_set_pos(wap_processbar_label_value, 0, 40);
	wa_obj_set_size(wap_processbar_label_value, WA_LCD_W, 16);
	wa_label_set_font(wap_processbar_label_value, &font_song_16_ascii);
	wa_label_set_align_mode(wap_processbar_label_value, WA_LABEL_AL_HCENTER | WA_LABEL_AL_VCENTER);
	sprintf(str, "%d", wap_processbar_value);
	wa_label_set_text(wap_processbar_label_value, str);
	
	wap_processbar_processbar = wa_processbar_create(NULL);
	wa_win_add_obj(wap_processbar_win, wap_processbar_processbar);
	wa_obj_set_pos(wap_processbar_processbar, 16, 28);
	wa_obj_set_size(wap_processbar_processbar, 96, 8);
	wa_processbar_set_direction(wap_processbar_processbar, WA_PROCESSBAR_DIR_LEFT);
	wa_processbar_set_limit_value(wap_processbar_processbar, 0, 100);
	wa_processbar_set_value(wap_processbar_processbar, wap_processbar_value);
	wa_processbar_set_back_color(wap_processbar_processbar, WA_COLOR_BACKGROUND);
	wa_processbar_set_fore_color(wap_processbar_processbar, WA_COLOR_FOREGROUND);
	wa_processbar_set_edge_size(wap_processbar_processbar, 1);
	wa_processbar_set_interval(wap_processbar_processbar, 1);
}

static void wap_processbar_del(void)
{
	wa_obj_del(wap_processbar_win);
}

static void _wap_processbar_paint(wa_obj* obj)
{
}

static void wap_processbar_callback(wa_sign* sign)
{
	int sign_id;
	wa_obj* obj;
	sign_id = sign->sign_id;
	obj = sign->obj_dst;

	switch (sign_id)
	{
		case WA_SIGNAL_PAINT:
		{
			_wap_processbar_paint(obj);
			break;
		}
		case WA_SIGNAL_NOTIFY_PARENT:
		{	
			break;
		}
		case WA_SIGNAL_USER_KEY_UP:
		{	
			char str[4];
			if((wap_processbar_value + 10) >= 100)
			{
				wap_processbar_value = 100;
			}
			else
			{
				wap_processbar_value += 10;
			}
			sprintf(str, "%d", wap_processbar_value);
			wa_label_set_text(wap_processbar_label_value, str);
			wa_processbar_set_value(wap_processbar_processbar, wap_processbar_value);
			break;
		}
		case WA_SIGNAL_USER_KEY_DOWN:
		{	
			char str[4];
			if(wap_processbar_value <= 10)
			{
				wap_processbar_value = 0;
			}
			else
			{
				wap_processbar_value -= 10;
			}
			sprintf(str, "%d", wap_processbar_value);
			wa_label_set_text(wap_processbar_label_value, str);
			wa_processbar_set_value(wap_processbar_processbar, wap_processbar_value);
			break;
		}
		case WA_SIGNAL_USER_KEY_LEFT:
		{	
			char str[4];
			if(wap_processbar_value > 0)
			{
				wap_processbar_value--;
			}
			sprintf(str, "%d", wap_processbar_value);
			wa_label_set_text(wap_processbar_label_value, str);
			wa_processbar_set_value(wap_processbar_processbar, wap_processbar_value);
			break;
		}
		case WA_SIGNAL_USER_KEY_RIGHT:
		{	
			char str[4];
			if(wap_processbar_value < 100)
			{
				wap_processbar_value++;
			}
			sprintf(str, "%d", wap_processbar_value);
			wa_label_set_text(wap_processbar_label_value, str);
			wa_processbar_set_value(wap_processbar_processbar, wap_processbar_value);
			break;
		}
		case WA_SIGNAL_USER_KEY_ESC:
		{	
			wa_screen_switch(WAP_WIN_MENU_ID, WA_SCREEN_SWITCH_TYPE_LEFT_TOGETHER, 500, wa_nok, wa_nok);
			break;
		}
		default:
		{
			break;
		}
	}
}

